package com.dx.mes.service.webservice.service;

import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.webservice.service.IFeppqcrptService;
import com.dx.mes.facade.webservice.tx.Base.BaseO;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlO;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlOA;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlOB;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlOC;
import com.ekingwin.www.esb.HNYG_049_SAP_PR_PP_0_ProSingleService.HNYG049_SAPPR_PP_0_ProSingleService_pttBindingQSServiceLocator;
import com.ekingwin.www.esb.HNYG_049_SAP_PR_PP_0_ProSingleService.HNYG049_SAPPR_PP_0_ProSingleService_pttBindingStub;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.rpc.holders.StringHolder;
import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;

/**
 * Created by admin on 2018/4/19.
 */
@Service("feppqcrptService")
public class FeppqcrptService implements IFeppqcrptService {
    private LogUtils logUtils;
    @Resource
    private MqOpe mqOpe;

    @Override
    public String subMainProc(String evt_no, String strInTrx) {
        AppContext.clear();
        AppContext.setCurrServiceName(FepwochngService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FepwochngService.class);
        logUtils.info("[InTrx:" + strInTrx + "]");
        String strOutTrx = "";
        BaseO outTrx = new BaseO();
        try {
            outTrx = transToErpProc(strInTrx);
        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg(ex.toString());
            logUtils.error(Util.stackTraceToString(ex));
        } finally {
            strOutTrx = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + strOutTrx + "]");
        }
        return strOutTrx;
    }

    public BaseO transToErpProc(String strInTrx) throws Exception {
        BaseO outTrx = new BaseO();

        List<EsbwsdlOC> iTEM_list = new ArrayList<EsbwsdlOC>();
        EsbwsdlOC iTEM = new EsbwsdlOC();
        iTEM.setDATAINFO(strInTrx);
        iTEM_list.add(iTEM);

        List<EsbwsdlOB> lIST_list = new ArrayList<EsbwsdlOB>();
        EsbwsdlOB lIST = new EsbwsdlOB();
        lIST.setITEM(iTEM_list);
        lIST_list.add(lIST);

        List<EsbwsdlOA> hEAD_list = new ArrayList<EsbwsdlOA>();
        EsbwsdlOA hEAD = new EsbwsdlOA();
        hEAD.setBIZTRANSACTIONID("SAP.PM_HNYG-049_" + Util.getMillisecondTime());
        hEAD.setCOUNT("1");
        hEAD.setSRVLEVEL("1");
        hEAD.setUSE("");
        hEAD.setCOMMENTS("");
        hEAD_list.add(hEAD);

        EsbwsdlO dATA = new EsbwsdlO();
        dATA.setHEAD(hEAD_list);
        dATA.setLIST(lIST_list);


        //调用ESB Webserivce
        HNYG049_SAPPR_PP_0_ProSingleService_pttBindingQSServiceLocator locator = new HNYG049_SAPPR_PP_0_ProSingleService_pttBindingQSServiceLocator();
        HNYG049_SAPPR_PP_0_ProSingleService_pttBindingStub stub = (HNYG049_SAPPR_PP_0_ProSingleService_pttBindingStub) locator.getHNYG049_SAPPR_PP_0_ProSingleService_pttBindingQSPort();
        StringHolder SIGN = new StringHolder();
        StringHolder message = new StringHolder();
        stub.HNYG049_SAPPR_PP_0_ProSingleService(mqOpe.obj2Xml(dATA, "DATA"), SIGN, message);
        if ("S".equals(SIGN.value)) {
            outTrx.setRtn_code(RETURN_CODE_OK);
            outTrx.setRtn_mesg(RETURN_MESG_OK);
        } else {
            outTrx.setRtn_code(SIGN.value);
            outTrx.setRtn_mesg(message.value);
        }
        return outTrx;
    }
}